package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* UTypeArrowView
* */
class UTypeArrowView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mStrokeWidth = context.resources.getDimension(R.dimen.dp_2)

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeWidth = mStrokeWidth
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_4DF1D8)
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeWidth = mStrokeWidth
            strokeCap = Paint.Cap.ROUND
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_4DF1D8)
        }
    }

    private val mPaint2 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.SQUARE
            strokeJoin = Paint.Join.MITER
            style = Paint.Style.FILL
            color = context.resources.getColor(R.color.color_4DF1D8)
        }
    }

    private val mPath1 by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            translate(cx, cy)
            save()
            translate(0F, -cy * 13 / 60)
            val arcX = cx * 2 / 5
            val arcY = cy * 2 / 5
            drawArc(
                -arcX,
                -arcY,
                arcX,
                arcY,
                180F,
                180F,
                false,
                mPaint0
            )

            mPath1.apply {
                reset()
                moveTo(-cx * 2 / 5, 0F)
                lineTo(-cx * 2 / 5, cy * 5 / 6)
            }
            drawPath(mPath1, mPaint1)

            mPath1.apply {
                reset()
                moveTo(cx * 2 / 5, 0F)
                lineTo(cx * 2 / 5, cy * 2 / 3)
            }
            drawPath(mPath1, mPaint1)

            mPath1.apply {
                reset()
                moveTo(cx * 2 / 5, cy * 5 / 6)
                lineTo(cx * 1 / 5,cy * 2 / 3)
                lineTo(cx * 3 / 5,cy * 2 / 3)
                close()
            }
            drawPath(mPath1, mPaint2)
            restore()
        }
    }
}